package com.service.bank.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.common.config.IdWorker;
import com.client.model.pojo.bank.ArBankcard;
import com.client.model.pojo.bank.ArElectronic;
import com.client.model.pojo.bank.Record;
import com.client.model.pojo.bank.TElectronicCard;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.deposit.ArBankcardVo;
import com.client.model.vo.deposit.ArElectoronicsVo;
import com.service.bank.mapper.ArBankcardMapper;
import com.service.bank.mapper.ArElectronicMapper;
import com.service.bank.mapper.RecordMapper;
import com.service.bank.service.TElectronicCardService;
import com.service.bank.mapper.TElectronicCardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.List;

/**
* @author liuwenju
* @description 针对表【t_electronic_card】的数据库操作Service实现
* @createDate 2024-04-01 20:55:27
*/
@Service
public class TElectronicCardServiceImpl extends ServiceImpl<TElectronicCardMapper, TElectronicCard>
    implements TElectronicCardService{

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private TElectronicCardMapper tElectronicCardMapper;

    @Autowired
    private ArBankcardMapper arBankcardMapper;

    @Autowired
    private ArElectronicMapper arElectronicMapper;

    @Autowired
    private RecordMapper recordMapper;


    @Override
    public void saveBankCard(ArBankcardVo arBankcardVo,String token) {
        // 参数校验判断参数是否为空
        if (ObjectUtil.isEmpty(arBankcardVo)){
            throw new RuntimeException("参数不能为空");
        }
        QueryWrapper<ArBankcard> queryWrapper = new QueryWrapper<>();
        //查询数据库 判断用户名是否存在
        queryWrapper.eq("real_name",arBankcardVo.getRealName());
        ArBankcard arBankcardName = this.arBankcardMapper.selectOne(queryWrapper);
        System.err.println(arBankcardName);
        if (ObjectUtil.isNull(arBankcardName)){
            throw new RuntimeException("用户名不存在");
        }

        //查询中间表数据库 判断用户名是否存在或已绑卡
        QueryWrapper<TElectronicCard> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("cid",arBankcardName.getCid());
        TElectronicCard tElectronicCard = this.tElectronicCardMapper.selectOne(queryWrapper1);
        if (!ObjectUtil.isEmpty(tElectronicCard)){
            throw new RuntimeException("用户名存在");
        }


        //查询数据库 判断银行卡号是否存在
        queryWrapper.eq("bank_card",arBankcardVo.getBankCard());
        ArBankcard arBankcardCard = this.arBankcardMapper.selectOne(queryWrapper);
        if (arBankcardCard==null){
            throw new RuntimeException("银行卡号不存在");
        }
        //判断数据库中的卡号和输入的卡号是否存在
        if(arBankcardCard.equals(arBankcardVo.getBankCard())){
            throw new RuntimeException("银行卡号存在");
        }
        //查询数据库 判断手机号是否存在
        queryWrapper.eq("moblie",arBankcardVo.getMoblie());
        ArBankcard arBankcardMobile = this.arBankcardMapper.selectOne(queryWrapper);
        //从redis中获取手机号
        if (arBankcardMobile==null ){
            throw new RuntimeException("手机号不存在");
        }

        // 拿到手机号和验证码
        String phone = arBankcardVo.getMoblie();
        String code = arBankcardVo.getCode();
        // 拿到redis中的验证码
        String code1 = this.redisTemplate.opsForValue().get(phone + "_" + code);
        // 判断验证码是否正确
        if (!arBankcardVo.getCode().equals(code1.toString())){
            throw new RuntimeException("验证码错误");
        }
        //查询到数据库中的盐
        String  slat = this.arBankcardMapper.SelectBankCard(arBankcardVo.getBankCard(),arBankcardVo.getMoblie());
        System.err.println(slat);
        //用户输入的密码加盐
        String password = DigestUtils.md5DigestAsHex((arBankcardVo.getPayPassword() + slat).getBytes());
        System.out.println("加密后的密码"+password);
        //查询到数据库中的密码
        String passwords  = this.arBankcardMapper.SelectPassword(arBankcardVo.getBankCard());
        System.out.println("数据库中的密码"+passwords);
        if (!passwords.equals(password)){
            throw new RuntimeException("密码错误");
        }else {
            //获取redis中存储的信息
            String userInfo = redisTemplate.opsForValue().get("user:login" + token);
            ArUser sysUser = JSON.parseObject(userInfo, ArUser.class);
            Long uid = sysUser.getUid();
            System.out.println(uid);
            // 插入数据库
            Long tid = this.arBankcardMapper.SelectByTid(arBankcardVo.getBankCard());
            Long cid = this.arBankcardMapper.SelectBycCid(arBankcardVo.getBankCard());
            Long eid = this.arElectronicMapper.SeectByUid(uid);
//            ArElectronic arElectronic = this.arElectronicMapper.selectByEid(arBankcardVo.getMoblie(),arBankcardVo.getBankCard());
            if (tid==1){
                this.tElectronicCardMapper.saveinsert(cid,eid,tid);
            }else {
                this.tElectronicCardMapper.saveinserts(cid,eid,2);
            }

        }

    }




    @Override
    public void savetixian(String token,ArElectoronicsVo arElectoronicsVo) {
        System.out.println("tokenqqqqqqqqqqqqqq" + token);
        String userInfo = redisTemplate.opsForValue().get("user:login" + token);
        ArUser sysUser = JSON.parseObject(userInfo, ArUser.class);
        //查询到数据库中的盐
        String salt = sysUser.getSalt();
        System.err.println(salt);
        //用户输入的密码加盐
        String password = DigestUtils.md5DigestAsHex((arElectoronicsVo.getPayPassword() + salt).getBytes());
        System.out.println("加密后的密码" + password);
        //查询到数据库中的密码
        String passwords = this.arElectronicMapper.SelectPass(arElectoronicsVo.getCard());
        System.out.println("数据库中的密码" + passwords);
        if (!passwords.equals(password)) {
            throw new RuntimeException("密码错误");
        } else {
            //查询到二类账户信息
            QueryWrapper<ArElectronic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("card", arElectoronicsVo.getCard());
            ArElectronic arElectronic = this.arElectronicMapper.selectOne(queryWrapper);
            System.err.println(arElectronic);
            //查询到银行卡信息
            QueryWrapper<ArBankcard> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("cid",arElectoronicsVo.getCid());
            ArBankcard arBankcard = this.arBankcardMapper.selectOne(queryWrapper1);

            //判断输入的金额是否大于余额
            if (arElectoronicsVo.getBalance().compareTo(arElectronic.getBalance()) == 1){
                throw new RuntimeException("输入金额不能大于余额");
            }

            //输入的金额必须大于零
            if (arElectoronicsVo.getBalance().compareTo(BigDecimal.ZERO) == -1){
                throw new RuntimeException("输入金额必须大于零");
            }

            //判断银行卡
            System.out.println("银行卡信息" + arElectronic.getDayDrawal().add(arElectoronicsVo.getBalance()));
            //判断数据中day_drawal字段和刚输入进来的数加起来是否大于一万
            if ((arElectronic.getDayDrawal().add(arElectoronicsVo.getBalance())).compareTo(new BigDecimal(10000.00)) > 0) {
                throw new RuntimeException("今日提现次数已用完");
            }
            //判断输入的金额是否大于10000
            if (arElectoronicsVo.getBalance().compareTo(new BigDecimal(10000)) == 1) {
                throw new RuntimeException("单次提现不能超过10000");
            }

            //添加每天提现次数到备用字段设置每天24小时
            arElectronic.setDayDrawal(arElectoronicsVo.getBalance().add(arElectronic.getDayDrawal()));
            arElectronic.setEid(arElectoronicsVo.getEid());
            this.arElectronicMapper.updateById(arElectronic);

            //判断数据库中的金额是否为零或成负数
                if (arElectronic.getBalance().compareTo(BigDecimal.ZERO) < 0) {
                    throw new RuntimeException("余额不足");
                }


            //输入金额减去二类表的金额
                BigDecimal subtract = arElectronic.getBalance().subtract(arElectoronicsVo.getBalance());
                ArElectronic arElectronic1 = new ArElectronic();
                arElectronic1.setEid(arElectronic.getEid());
                arElectronic1.setBalance(subtract);
                 this.arElectronicMapper.updateById(arElectronic1);

                 //输入金额加到银行卡表的金额
                 BigDecimal add = arBankcard.getBalance().add(arElectoronicsVo.getBalance());
                ArBankcard arBankcard1 = new ArBankcard();
                arBankcard1.setCid(arBankcard.getCid());
                arBankcard1.setBalance(add);
                this.arBankcardMapper.updateById(arBankcard1);

                //插入记录表
            Record record = new Record();
                IdWorker idWorker = new IdWorker();
                record.setRid(idWorker.nextId());
                record.setMainCard(arBankcard.getBankCard());
                record.setToCard(arElectoronicsVo.getCard());
                record.setBalance(arElectoronicsVo.getBalance());
                record.setOperatorStatus(2);
                this.recordMapper.insert(record);
        }
    }



}




